package com.summer.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicInteger;

public class CyclicBarrierTest {

	public static void main(String[] args) {
		BarrierTask task1 = new BarrierTask();
		BarrierTask task2 = new BarrierTask();
		BarrierTask task3 = new BarrierTask();
		
		task1.start();
		task2.start();
		task3.start();
//		try {
//			BarrierTask.cyclicBarrier.await();
//			System.out.println(BarrierTask.number);
//		} catch (InterruptedException | BrokenBarrierException e) {
//			e.printStackTrace();
//		}
	}
	
	public static class BarrierTask extends Thread{
		private static Runnable completeTask = new Runnable() {
			@Override
			public void run() {
				System.out.println("completeTask:"+number);
			}
		};
		private static final CyclicBarrier cyclicBarrier = new CyclicBarrier(3,completeTask);
		private static final AtomicInteger number = new AtomicInteger(0);
		@Override
		public void run() {
			try {
				number.incrementAndGet();
				cyclicBarrier.await();
				System.out.println(Thread.currentThread().getName()+":"+number);
			} catch (InterruptedException|BrokenBarrierException e) {
				e.printStackTrace();
			}
		}
		

	}
	
}
